#include "Equipment.h"
#include "Weapon.h"
#include "Item.h"

Equipment::Equipment() {
	for(EquipmentLocation i = EL_LEFT_HAND; i < NUM_EQUIPMENT_LOCATIONS; i = EquipmentLocation(int(i)+1))
		m_equipment[i] = NULL;
}

Equipment::~Equipment() {
	unequipAll();
	m_equipment.clear();
}

bool Equipment::equip(EquipmentLocation location, Item* item){
	if((location == EL_LEFT_HAND && isEquipped(EL_RIGHT_HAND) && ((Weapon::isWeapon(item) && ((Weapon*)item)->getGripType() == GT_TWO)
		|| (Weapon::isWeapon(get(EL_RIGHT_HAND)) && ((Weapon*)get(EL_RIGHT_HAND))->getGripType() == GT_TWO)))
		|| (location == EL_RIGHT_HAND && isEquipped(EL_LEFT_HAND) && ((Weapon::isWeapon(item) && ((Weapon*)item)->getGripType() == GT_TWO)
			|| (Weapon::isWeapon(get(EL_LEFT_HAND)) && ((Weapon*)get(EL_LEFT_HAND))->getGripType() == GT_TWO))))
		return false;
	item->setIsEquipped(true, location);
	m_equipment[location] = item;
	return true;
}

bool Equipment::unequip(EquipmentLocation location) {
	Item* item = m_equipment[location];
	if(item->getStatus() == CURSED) return false;
	item->setIsEquipped(false, NUM_EQUIPMENT_LOCATIONS);
	m_equipment[location] = NULL;
	return true;
}

bool Equipment::isEquipped(EquipmentLocation location) const {
	if(m_equipment.at(location)) return true;
	return false;
}

EquipmentLocation Equipment::whereEquipped(Item* item) const {
	for(EquipmentLocation i = EL_LEFT_HAND; i < NUM_EQUIPMENT_LOCATIONS; i = EquipmentLocation(int(i)+1)) {
		if(m_equipment.at(i) == item) return i;
	}
	return NUM_EQUIPMENT_LOCATIONS;
}

void Equipment::unequipAll() {
	for(int i = 0; i < NUM_EQUIPMENT_LOCATIONS; i++) {
		if(m_equipment[EquipmentLocation(i)])
			m_equipment[EquipmentLocation(i)]->setIsEquipped(false, NUM_EQUIPMENT_LOCATIONS);
		m_equipment[EquipmentLocation(i)] = NULL;
	}
}
